package com.gpscookie.mobile;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import de.enough.polish.ui.Spacer;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.TextField;
import de.enough.polish.util.Locale;

public class ForumPostScreen extends BasicScreen implements DownloadListener
{

	private DownloadManager dlManager = null;
	private Command cmdSave;
	private StringItem labInfo;
	private TextField txtDetail;
	private Display display;
	private String url = "http://gpscookie.appspot.com/mobile/forum/post";
	private JSONObject mission = null;
	private String secret = null;
	
	
	public ForumPostScreen(String title, GPSCookie globalClass) {
		super(title, globalClass);
		this.dlManager = new DownloadManager();
		
		//#style textFieldBig		
		txtDetail = new TextField(Locale.get("DESCRIPTION"), "", 200, TextField.ANY);
		append(txtDetail);
	
		this.cmdBack = new Command(Locale.get("BACK"), Command.SCREEN, 20);		
		this.cmdSave = new Command(Locale.get("SAVE"), Command.SCREEN, 10);
		this.addCommand(this.cmdSave);
		this.addCommand(this.cmdBack);	
		this.parentScreen = GPSCookie.forumListScreen;	
		
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("READY"));
		this.append(this.labInfo);
		
	}

	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == cmdBack) {
			this.goBack();
		} else if (arg0 == cmdSave) {
			this.savePost();
		}
	}
	
	private void cleanScreen() {
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("WAITMSG"));
		this.append(this.labInfo);		
	}
	
	public void downloadCompleted(byte[] data) {		
		String str = "";
		try {
			if (data==null || data.length<=0) {
				this.labInfo.setText(Locale.get("ERROR_DOWNLOAD"));
				return;
			}
			str = GPSCookie.http2String(data);
			JSONObject json = new JSONObject(str);
			String msg = json.getString("msg");
			if ("OK".equals(msg.toUpperCase())) {
				this.labInfo.setText(Locale.get("READY"));
			} else {
				this.labInfo.setText(Locale.get("ERROR_SAVE"));
			}
			this.globalClass.forumList(mission, secret);
		} catch (Exception e) {
			System.out.println(e.toString());
			this.labInfo.setText(Locale.get("ERROR_DOWNLOAD"));
		}
	}
	
	public void downloadError(Exception e) {
		this.labInfo.setText(e.getMessage());	
	}

	public void downloadStatus(int percent) {
		this.labInfo.setText(String.valueOf(percent)+"%");		
	}

	private void savePost() {
		this.labInfo.setText(Locale.get("WAITMSG"));
		try {
			this.dlManager.upload(this, url, composePayload());
		} catch (Exception e) {
			this.labInfo.setText(Locale.get("ERROR_SAVE"));
		}
	}
	
	private byte[] composePayload() throws Exception
	{
		RMSStore store = GPSCookie.rmsstore;
		String rtn = "mission="+mission.getString("dbid");
		String user = store.get(RMSStore.USER, "");
		if (user==null || user.length()==0) {
			throw new Exception("no user found");
		}
		rtn += "&customer="+user;
		rtn += "&comments="+GPSCookie.string2http(this.txtDetail.getString());
		System.out.println(rtn);
		return rtn.getBytes();
	}

	public void setMission(JSONObject mission, String secret) {
		this.mission = mission;		
		this.secret = secret;
	}	
}
